//
//  WDWorderOrderMainVehDetailViewModel.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/3.
//

#import "WDWorderOrderMainVehDetailViewModel.h"
#import "WDWorderOrdMainVehDetailServiceView.h"

@implementation WDWorderOrderMainVehDetailViewModel

- (void)initialize {
    [super initialize];
    self.title = @"工单详情";
}

/// 获取详情
/// - Parameter completion: completion description
- (void)requestWithTaskInfoDetail:(RequestCompletionBlock)completion {
    @weakify(self);
    WDTaskInfoDetailRequest *request = [[WDTaskInfoDetailRequest alloc] initWithTaskId:@(self.serviceModel.id)];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            self.taskDetailModel = respond.data;
            
            UIFont *font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
            float width = kScreenW - 20 * 2 - 102;
            float height = [self.taskDetailModel.detail heightForFont:font width:width];
            height = (height > 18 ? height : 18);
            self.taskDetailModel.detailHeight = height;
            
            /*
            NSData *jsonData = [self.taskDetailModel.maintainItems dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error = nil;
            id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
            if (!error && jsonObj) {
                NSMutableArray *addList = [NSMutableArray new];
                if ([jsonObj isKindOfClass:[NSArray class]]) {
                    NSArray *list = (NSArray *)jsonObj;
                    for (NSDictionary *dict in list) {
                        MaintainItemModel *model = [MaintainItemModel yy_modelWithJSON:dict];
                        [addList addObject:model];
                    }
                } else if ([jsonObj isKindOfClass:[NSDictionary class]]) {
                    MaintainItemModel *model = [MaintainItemModel yy_modelWithJSON:jsonObj];
                    [addList addObject:model];
                }
                self.taskDetailModel.maintainItemList = addList.copy;
            }
            
            // 维修项目RepairItemModel
            jsonData = [self.taskDetailModel.repairItems dataUsingEncoding:NSUTF8StringEncoding];
            
            jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
            if (!error && jsonObj) {
                NSMutableArray *addList = [NSMutableArray new];
                if ([jsonObj isKindOfClass:[NSArray class]]) {
                    NSArray *list = (NSArray *)jsonObj;
                    for (NSDictionary *dict in list) {
                        RepairItemModel *model = [RepairItemModel yy_modelWithJSON:dict];
                        [addList addObject:model];
                    }
                } else if ([jsonObj isKindOfClass:[NSDictionary class]]) {
                    RepairItemModel *model = [RepairItemModel yy_modelWithJSON:jsonObj];
                    [addList addObject:model];
                }
                self.taskDetailModel.repairItemList = addList.copy;
            }*/
            
            /*
            NSMutableArray *addPhoto = [NSMutableArray new];
            WDInspectUploadModel *ent = [WDInspectUploadModel new];
            ent.uploadUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2025%2F0124%2Ff3baf675j00sqkid5000rd000990099g.jpg&thumbnail=140y88&quality=100&type=jpg";
            [addPhoto addObject:ent];
            ent = [WDInspectUploadModel new];
            ent.uploadUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fcms-bucket.ws.126.net%2F2025%2F0124%2F403dd08ep00sqkeie001lc0009c0070c.png&thumbnail=140y88&quality=100&type=jpg";
            [addPhoto addObject:ent];
            ent = [WDInspectUploadModel new];
            ent.uploadUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fcms-bucket.ws.126.net%2F2025%2F0123%2F41260445p00sqjqg30018c0009c0070c.png&thumbnail=140y88&quality=100&type=jpg";
            [addPhoto addObject:ent];
            ent = [WDInspectUploadModel new];
            ent.uploadUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fcms-bucket.ws.126.net%2F2025%2F0123%2F78de9eb8p00sqjmtm001lc0009c0070c.png&thumbnail=140y88&quality=100&type=jpg";
            [addPhoto addObject:ent];
            self.taskDetailModel.problemPhotoList = [addPhoto copy];
            */
            
            
            for (WDCanFaultInfoModel *ent in self.taskDetailModel.canFaultInfoList) {
                NSString *str = [NSDate timeStampWithString:ent.stimets format:@"yyyy-MM-dd HH:mm:ss"];
                ent.stimetsStr = str;
            }
            
            
            float imageWidth = (kScreenW - 20 * 2 - 16 * 2) / 3.0;
            float imageHeight = imageWidth / 1.42857;
            
            width = kScreenW - 122 - 20;
            font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
            
            for (WDTaskFlowModel *flowModel in self.taskDetailModel.taskFlowList) {
                [flowModel.files removeAllObjects];
                [flowModel.otherFiles removeAllObjects];
                [flowModel.finishFiles removeAllObjects];
                
                flowModel.scanAddressHeight = [flowModel.scanAddress heightForFont:font width:width] + 2;
                if (flowModel.scanAddressHeight < 18) {
                    flowModel.scanAddressHeight = 18;
                }
                flowModel.workExplainHeight = [flowModel.workExplain heightForFont:font width:width] + 2;
                if (flowModel.workExplainHeight < 18) {
                    flowModel.workExplainHeight = 18;
                }
                
                if (flowModel.servOperate == 1) { // 接车
                    self.taskDetailModel.meetCarFlowModel = flowModel;
                } else if (flowModel.servOperate == 2) { // 检查
                    NSArray *types = @[@1, @2, @3, @4, @5];
                    for (WDInspectUploadModel *img in flowModel.photoList) {
                        // 上传类型 0默认 检查:1VIN码 2车辆仪表 3车头45度 4故障部位 5行驶证 6其他 维修:1新旧件对比 2车辆换件前 3车辆换件后 4其他 5完成
                        if ([types containsObject:@(img.uploadType)]) {
                            [flowModel.files addObject:img];
                        } else if (img.uploadType == 6) { // 其它
                            [flowModel.otherFiles addObject:img];
                        }
                    }
                    for (WDInspectUploadModel *video in flowModel.videoList) {
                        if ([types containsObject:@(video.uploadType)]) {
                            [flowModel.files addObject:video];
                        } else if (video.uploadType == 6) { // 其它
                            [flowModel.otherFiles addObject:video];
                        }
                    }
                    self.taskDetailModel.inspectFlowModel = flowModel;
                } else if (flowModel.servOperate == 4) { // 继续维修
                    // 维修:1新旧件对比 2车辆换件前 3车辆换件后 4其他 5完成
                    NSArray *types = @[@1, @2, @3];
                    for (WDInspectUploadModel *img in flowModel.photoList) {
                        if ([types containsObject:@(img.uploadType)]) {
                            [flowModel.files addObject:img];
                        } else if (img.uploadType == 4) { // 其它
                            [flowModel.otherFiles addObject:img];
                        } else if (img.uploadType == 5) { // 完成
                            [flowModel.finishFiles addObject:img];
                        }
                    }
                    for (WDInspectUploadModel *video in flowModel.videoList) {
                        debugLog(@"uploadType=%zd", video.uploadType);
                        if ([types containsObject:@(video.uploadType)]) {
                            [flowModel.files addObject:video];
                        } else if (video.uploadType == 4) { // 其它
                            [flowModel.otherFiles addObject:video];
                        } else if (video.uploadType == 5) { // 完成
                            [flowModel.finishFiles addObject:video];
                        }
                    }
                    
                    // 指定图片
                    float imageTitleHeight = imageHeight + 5 + 18;
                    flowModel.filesHeight = 0;
                    if (flowModel.files.count > 0) {
                        flowModel.filesHeight = 18 + 12 + imageTitleHeight;
                    }
                    
                    // 其它图片
                    if (flowModel.otherFiles.count == 0) {
                        flowModel.otherFilesHeight = 0;
                    } else {
                        int row = ceilf(flowModel.otherFiles.count / 3.0);
                        flowModel.otherFilesHeight = 10 + 18 + 12 + row * imageHeight + (row - 1) * 10;
                    }
                    
                    // 完成图片
                    if (flowModel.finishFiles.count == 0) {
                        flowModel.finishFilesHeight = 0;
                    } else {
                        int row = ceilf(flowModel.finishFiles.count / 3.0);
                        flowModel.finishFilesHeight = 10 + 18 + 12 + row * imageHeight + (row - 1) * 10;
                    }
                    
                    flowModel.flowHeight = kWDTaskDetailBasicSingleViewHeight * 3 - 18 + flowModel.workExplainHeight + 9 + flowModel.filesHeight + (flowModel.filesHeight > 0 ? 18 : 0) + flowModel.otherFilesHeight + (flowModel.otherFilesHeight > 0 ? 0 : 0) + flowModel.finishFilesHeight + (flowModel.finishFilesHeight > 0 ? 0 : 0) + kWDTaskDetailBasicSingleViewHeight * 2 + 12;
                    
                    [self.taskDetailModel.tmpTaskFlows addObject:flowModel];
                }
            }
            
            
//            float width = kScreenW - 122 - 20;
//            UIFont *font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
            /*
            self.taskDetailModel.vehicleAds = @"河南省洛阳市朱雀大街与长兴街交口北350M左路口";
            self.taskDetailModel.vehicleAdsHeight = [self.taskDetailModel.vehicleAds heightForFont:font width:width] + 2;
            if (self.taskDetailModel.vehicleAdsHeight < 18) {
                self.taskDetailModel.vehicleAdsHeight = 18;
            }*/
            
//            self.taskDetailModel.workExplain = @"雨刮器需要直接更换，左侧前大灯接触不良需要维修，目前发现驾驶室有轻微晃动，排查为发现愿意，需要厂家支援！";
//            self.taskDetailModel.workExplainHeight = [self.taskDetailModel.workExplain heightForFont:font width:width] + 2;
//            if (self.taskDetailModel.workExplainHeight < 18) {
//                self.taskDetailModel.workExplainHeight = 18;
//            }
            
            /*
            NSMutableArray *addList = [NSMutableArray new];
            WDBatteryInfoModel *model = [WDBatteryInfoModel new];
            model.batteryTypeValue = @"卡里锂电池"; // 电池类型
            WDBatteryModelModel *ent1 = [WDBatteryModelModel new];
            model.batteryModel = ent1;
            ent1.modelNo = @"KT2424242";// 型号
            [addList addObject:model];
            [addList addObject:model];
            self.taskDetailModel.batteryList = addList.copy;
            */
            
//            self.taskDetailModel.otherImageVideos = @[@"1", @"2", @"3", @"4"];
            
            
            self.taskDetailModel.imageWidth = imageWidth;
            self.taskDetailModel.imageHeight = imageHeight;
            self.taskDetailModel.inspectFlowModel.imageWidth = imageWidth;
            self.taskDetailModel.inspectFlowModel.imageHeight = imageHeight;
            NSInteger count = self.taskDetailModel.inspectFlowModel.otherFiles.count;
            if (count == 0) {
                self.taskDetailModel.inspectFlowModel.otherFilesHeight = 0;
            } else {
                int row = ceilf(count / 3.0);
                self.taskDetailModel.inspectFlowModel.otherFilesHeight = 10 + 18 + 12 + row * imageHeight + (row - 1) * 10;
            }
            
//            debugLog(@"row=%d", row);
//            debugLog(@"ddd");
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
